package com.app.service;

import java.util.List;
import java.util.Map;

import javax.jws.soap.SOAPBinding.Use;
import javax.xml.bind.ValidationException;

import com.app.responseVO.UserResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.app.dao.UserDao;
import com.app.entity.User;
import com.app.entity.UserHome;
import com.app.exception.ValidateException;
import com.app.responseVO.PageVO;
import com.app.responseVO.ReturnResultResponseVO;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    /**
     * 登录校验
     *
     * @param user
     * @return
     * @throws ValidateException
     */
    public boolean doLogin(User user) throws ValidateException {
        try {
            User userFromData = userDao.selectUser(user.getUsername(),user.getPassword());
            if (null == userFromData) {
                throw new ValidateException("用户名不存在！");
            }
            if (1 == userFromData.getUserState()) {
                throw new ValidateException("用户被停用请联系管理员！");
            }
            if (user.getPassword().equals(userFromData.getPassword())) {
                return true;
            }
        } catch (ValidateException e) {
            throw e;
        } catch (Exception e) {
            throw new ValidateException("内部错误！");
        }
        return false;
    }
    
    /**
     * 管理员登录校验
     *
     * @param user
     * @return
     * @throws ValidateException
     */
    public boolean doAdminLogin(User user) throws ValidateException {
        try {
            User userFromData = userDao.selectAdmin(user.getUsername());
            if (null == userFromData) {
                throw new ValidateException("用户名不存在！");
            }

            if (user.getPassword().equals(userFromData.getPassword())) {
                return true;
            }
        } catch (ValidateException e) {
            throw e;
        } catch (Exception e) {
            throw new ValidateException("内部错误！");
        }
        return false;
    }

    /**
     * 分页获取用户信息
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    public ReturnResultResponseVO<User> getUserList(int currentPage, int pageSize,String userName) throws ValidationException {

        ReturnResultResponseVO<User> responseData = new ReturnResultResponseVO<User>();
        int countPage = 0;
        int count = userDao.userCount();
        try {
            if (count % pageSize == 0) {
                countPage = count / pageSize;
            } else {
                countPage = count / pageSize + 1;
            }
            if (currentPage < 1) {
                currentPage = 1;
            }
            if(countPage == 0) {
                countPage = 1;
            }
            if (currentPage > countPage) {
                currentPage = countPage;
            }
            PageVO pageVO = new PageVO();
            pageVO.setCountPage(countPage);
            pageVO.setCurrentPage(currentPage);
            pageVO.setPagesize(pageSize);
            responseData.setPage(pageVO);
            List<User> users = userDao.selectUserByPage((currentPage - 1) * pageSize, pageSize, userName);
            responseData.setData(users);
        } catch (Exception e) {
            throw new ValidationException("获取用户列表异常");
        }
        return responseData;

    }
    public boolean updateUserFee(Map<String, Object> oFee){
    	int flag = userDao.updateUserFee(oFee);
    	return flag>0;
    }
    /**
     * 获取用户详情
     *
     * @param userName
     * @return
     */
    public UserHome getUserHome(String userName) {
        UserHome userHome = userDao.selectUserDetail(userName);
        return userHome;
    }

    /**
     * 新增用户
     *
     * @param userName
     * @param password
     */
    @Transactional(rollbackFor = ValidateException.class)
    public void addUser(String userName, String password) {
        User user = userDao.selectUser(userName,null);
        if (null != user) {
            throw new ValidateException("用户名已存在");
        }
        userDao.addUser(userName, password);
        userDao.addUserHome(userName);
    }

    /**
     * 更新用户状态
     *
     * @param userName
     * @param statues
     */
    public void updateStatus(String userName, Integer statues) {
        try {
            userDao.updateStatue(userName, statues);
        } catch (Exception e) {
            throw new ValidateException("更新用户异常！");
        }
    }

    /**
     * 删除用户
     *
     * @param userName
     */
    public void deleteUser(String userName) {
        try {
            userDao.deleteUser(userName);
        } catch (Exception e) {
            throw new ValidateException("删除用户异常！");
        }
    }
    /**
     * 更新更新用户信息
     * @param userHome
     */
    public void updateUserHome(UserHome userHome) {
        try {
            userDao.updateUserHome(userHome);
        } catch (Exception e) {
            throw new ValidateException("更新用户home异常");
        }
    }
}
